#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void destory_acc(void)
{
	char path[PATH_MAX];
	Account acc;
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	if(ERROR == fd)
	{
		perror("open acc");
		sprintf(stoc.str,"对不起，该用户不存在");
	}
	else
	{
		read(fd,&acc,sizeof(Account));
		if(acc.errno > 3)
		{
			sprintf(stoc.str,"对不起，该账户已被锁定，请先解锁");
		}
		else if(ctos.acc.pass == acc.pass)
		{
			sprintf(stoc.str,"恭喜您销户成功");
			remove(path);
		}
		else
		{
			sprintf(stoc.str,"对不起，您的密码错误");
		}
		
	}
	// 返回开户成功信息
	stoc_send(&stoc);
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_DESTORY);
		stoc.type = ctos.pid;
		destory_acc();
	}
}
